from flask import request, Blueprint, json
from services import service_volunteer as service
from util import mapping

volunteer_blue = Blueprint('volunteer', __name__)


@volunteer_blue.route('/volunteer', methods=['get'])
def getVolunteer():
    data = service.getAll()
    return data


@volunteer_blue.route('/volunteer/<id>', methods=['get'])
def getVolunteerById(id):
    data = service.getById(id)
    return data


@volunteer_blue.route('/volunteer', methods=['post'])
def addVolunteer():
    request_data = request.get_json()  # 格式 {'id': 169, 'name': '逄无敌', 'mobile': '18680343705', 'address': '536', 'email': '54@qq.com', 'gender': '女', 'age': '542'}
    data = service.add(request_data)
    return data


@volunteer_blue.route('/volunteer', methods=['put'])
def UpdateVolunteer():
    request_data = request.get_json()
    request_data = mapping.dictToVolDic(request_data)
    data = service.update(request_data)
    return data


@volunteer_blue.route('/volunteer/<ID>', methods=['delete'])
def deleteVolunteer(ID):
    data = service.deleteByID(ID)
    return data
